package com.dzh.xdyy;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Robot {

    private static String TOKEN="x4m8xU9co9KQbF3m23SNfs3yima9WI";
    public static Cache<String,String> cache=new Cache<>();

    public static String sign(){
        Map<String, String> params = new HashMap<String, String>();
        params.put("userid", "bWLlZGaRq8j");
        String jsonstr=HttpUtils.post("https://openai.weixin.qq.com/openapi/sign/"+TOKEN,params,"utf-8");
        try {
            return new JSONObject(jsonstr).getString("signature");
        } catch (Exception e) {
            return "";
        }
    }

    public static String aibot(String query){
        String signature=cache.get("sign");
        if(signature==null){
            signature=sign();
            cache.put("sign",signature,7000000);
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("signature", signature);
        params.put("query",query);
        params.put("env","debug");
        String jsonstr=HttpUtils.post("https://openai.weixin.qq.com/openapi/aibot/"+TOKEN,params,"utf-8");
        try {
            return new JSONObject(jsonstr).getString("answer");
        } catch (Exception e) {
            return "接口返回失败";
        }
    }
}
